package edu.chl.dat255.bluebanana.logic.impl;

import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import edu.chl.dat255.bluebanana.data.impl.DbConst;
import edu.chl.dat255.bluebanana.data.impl.SQLiteStorage;
import edu.chl.dat255.bluebanana.logic.ICommunication;
import edu.chl.dat255.bluebanana.logic.IProfileHandler;
import edu.chl.dat255.bluebanana.logic.IScreen;
import edu.chl.dat255.bluebanana.logic.ISound;
import edu.chl.dat255.bluebanana.presentation.IProfileView;
import edu.chl.dat255.bluebanana.presentation.impl.ProfileView;
import edu.chl.dat255.bluebanana.utils.DatabaseUtil;
import edu.chl.dat255.bluebanana.utils.Observable;
import edu.chl.dat255.bluebanana.utils.ObservableAdapter;
import edu.chl.dat255.bluebanana.utils.Observer;

public class ProfileHandler implements IProfileHandler {
	private long activeId = -1;
	private long savedSystemSettingsId = 1;
	private ICommunication communication;
	private IScreen screen;
	private ISound sound;
	private Context context;
	public static final String DISABLE_ALL_PROFILES_ACTION = "edu.chl.dat255.bluebanana.action.DISABLE_ALL_PROFILES";
	private ObservableAdapter obsAda;

	public ProfileHandler(Context context) {
		screen = APScreen.getInstance(context);
		sound = APSound.getInstance(context);
		communication = APCommunication.getInstance(context);
		this.context = context;
		obsAda = new ObservableAdapter();

		savedSystemSettingsId = DatabaseUtil
				.createSystemSettingsProfile(context);
	}

	@Override
	public void update(Observable parent, Object state) {
		long id = (Long) state;
		ProfileView view = (ProfileView) parent;
		if (activeId == -1) {
			saveCurrentSystemSettings();
		}

		if (activeId == id) {
			deactivateProfile(view, id);
			restoreSystemSettings();
		} else {
			activateProfile(view, id);
		}
	}

	private void saveCurrentSystemSettings() {
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_SCREEN_AUTO_BRIGHTNESS_ENABLE,
				screen.isAutoBrightness(), context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_SCREEN_BRIGHTNESS, screen.getBrightness(), context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_SCREEN_TIMEOUT, screen.getScreenTimeOut(), context);

		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_BLUETOOTH_ENABLE, communication.isBluetooth(),
				context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_FLIGHT_MODE_ENABLE, communication.isPacketData(),
				context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_WIFI_ENABLE, communication.isWifi(), context);

		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_MESSAGETONE_PATH, sound.getNotificationTone(),
				context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_NOTIFICATION_VOLUME, sound.getNotificationVolume(),
				context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_RINGTONE_PATH, sound.getRingtone(), context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_CALL_VOLUME, sound.getRingVolume(), context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_MUTE_ENABLE, sound.isSilent(), context);
		DatabaseUtil.saveSystemSetting(savedSystemSettingsId,
				DbConst.KEY_VIBRATE_ENABLE, sound.isVibrate(), context);
		// TODO: turn off auto message, block calls
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void activateProfile(ProfileView view, long id) {
		notifyObservers(id);
		activeId = id;
		view.setBackgroundColor(Color.LTGRAY);

		screen.setAutoBrightness(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_SCREEN_AUTO_BRIGHTNESS_ENABLE, context));
		screen.setBrightness(DatabaseUtil.getIntValue(id,
				DbConst.KEY_SCREEN_BRIGHTNESS, context));
		screen.setScreenTimeout(DatabaseUtil.getIntValue(id,
				DbConst.KEY_SCREEN_TIMEOUT, context));

		communication.setAutoRespond(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_AUTO_MESSAGE_ENABLE, context));
		communication.setAutoRespondMessage(DatabaseUtil.getTextValue(id,
				DbConst.KEY_AUTO_MESSAGE_CONTENT, context));
		communication.setBlockCalls(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_BLOCKED_CALLS, context));
		communication.setBluetooth(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_BLUETOOTH_ENABLE, context));
		communication.setFlightMode(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_FLIGHT_MODE_ENABLE, context));
		communication.setPacketData(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_PACKET_DATA_ENABLE, context));
		communication.setWifi(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_WIFI_ENABLE, context));

		sound.setNotificationRingtone(DatabaseUtil.getTextValue(id,
				DbConst.KEY_MESSAGETONE_PATH, context));
		sound.setNotificationVolume(DatabaseUtil.getIntValue(id,
				DbConst.KEY_NOTIFICATION_VOLUME, context));
		sound.setRingtone(DatabaseUtil.getTextValue(id,
				DbConst.KEY_RINGTONE_PATH, context));
		sound.setRingVolume(DatabaseUtil.getIntValue(id,
				DbConst.KEY_CALL_VOLUME, context));
		sound.setSilentMode(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_MUTE_ENABLE, context));
		sound.setVibrate(DatabaseUtil.getBooleanValue(id,
				DbConst.KEY_VIBRATE_ENABLE, context));
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void deactivateProfile(ProfileView view, long id) {
		activeId = -1;
		view.setBackgroundColor(Color.BLACK);
	}

	private void restoreSystemSettings() {
//		Cursor c = DatabaseUtil.getCurrentSystemProfile(context);
		SQLiteStorage db = new SQLiteStorage(context);
		db.open();

		Cursor c = db.getCurrentSystemProfile();

		try {
			if (c != null && c.moveToNext()) {
				screen.setAutoBrightness(c.getInt(c
						.getColumnIndex(DbConst.KEY_SCREEN_AUTO_BRIGHTNESS_ENABLE)) == 1);
				screen.setBrightness(c.getInt(c
						.getColumnIndex(DbConst.KEY_SCREEN_BRIGHTNESS)));
				screen.setScreenTimeout(c.getInt(c
						.getColumnIndex(DbConst.KEY_SCREEN_TIMEOUT)));

				communication.setAutoRespond(c.getInt(c
						.getColumnIndex(DbConst.KEY_AUTO_MESSAGE_ENABLE)) == 1);
				communication.setAutoRespondMessage(c.getString(c
						.getColumnIndex(DbConst.KEY_AUTO_MESSAGE_CONTENT)));
				communication.setBlockCalls(c.getInt(c
						.getColumnIndex(DbConst.KEY_BLOCKED_CALLS)) == 1);
				communication.setBluetooth(c.getInt(c
						.getColumnIndex(DbConst.KEY_BLUETOOTH_ENABLE)) == 1);
				communication.setFlightMode(c.getInt(c
						.getColumnIndex(DbConst.KEY_FLIGHT_MODE_ENABLE)) == 1);
				communication.setPacketData(c.getInt(c
						.getColumnIndex(DbConst.KEY_PACKET_DATA_ENABLE)) == 1);
				communication.setWifi(c.getInt(c
						.getColumnIndex(DbConst.KEY_WIFI_ENABLE)) == 1);

				sound.setNotificationRingtone(c.getString(c
						.getColumnIndex(DbConst.KEY_MESSAGETONE_PATH)));
				sound.setNotificationVolume(c.getInt(c
						.getColumnIndex(DbConst.KEY_NOTIFICATION_VOLUME)));
				sound.setRingtone(c.getString(c
						.getColumnIndex(DbConst.KEY_RINGTONE_PATH)));
				sound.setRingVolume(c.getInt(c
						.getColumnIndex(DbConst.KEY_CALL_VOLUME)));
				sound.setSilentMode(c.getInt(c
						.getColumnIndex(DbConst.KEY_MUTE_ENABLE)) == 1);
				sound.setVibrate(c.getInt(c
						.getColumnIndex(DbConst.KEY_VIBRATE_ENABLE)) == 1);
			}
		} finally {
			if (c != null)
				c.close();
				db.close();
		}
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public long getActiveProfileId() {
		return activeId;
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void deactivateAllProfiles(ArrayList<IProfileView> profileViews) {
		for (IProfileView profileView : profileViews) {
			deactivateProfile((ProfileView) profileView, profileView.getRowId());
		}
	}

	@Override
	public boolean addObserver(Observer o) {
		return obsAda.addObserver(o);
	}

	@Override
	public boolean deleteObserver(Observer o) {
		return obsAda.deleteObserver(o);
	}

	@Override
	public void deleteObservers() {
		obsAda.deleteObservers();
	}

	@Override
	public boolean hasChanged() {
		return obsAda.hasChanged();
	}

	@Override
	public void notifyObservers(Object state) {
		obsAda.setChanged();
		obsAda.notifyObservers(state);
	}
}
